/**
 * @author 雷明浩
 * @date 2025/7/18 15:28
 */
public class LongestCommonPrefix {
    // 最长公共前缀
    // https://www.nowcoder.com/practice/28eb3175488f4434a4a6207f6f484f47?tpId=295&tqId=732&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D295
    public String longestCommonPrefix (String[] strs) {
        // write code here
        if (strs.length == 0) {
            return "";
        }
        StringBuilder ret = new StringBuilder(strs[0]);
        for (int i = 1; i < strs.length; i++) {
            int index = 0;
            for (int j = 0; j < strs[i].length() && j < ret.length(); j++) {
                if (ret.charAt(j) == strs[i].charAt(j)) {
                    index++;
                } else {
                    break;
                }
            }
            ret = new StringBuilder(ret.substring(0, index));
        }
        return ret.toString();
    }
}